import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicStampedReference;

/**
 * @author brianxia
 * @version 1.0
 * @date 2022-11-16 14:38
 */
public class Demo5 {

    public static AtomicInteger count = new AtomicInteger(0);
    //解决ABA问题
    public static AtomicStampedReference<String> atomicStampedReference =
            new AtomicStampedReference("",1);

    public static void main(String[] args) throws InterruptedException {
        String name = Thread.currentThread().getName();
        System.out.println(name);
        int i = 1; //线程私有变量
        int totalCount = 100000;
        Thread thread1 = new Thread(() -> {
            for (int j = 0; j < totalCount; j++) {
                count.incrementAndGet();
            }
        });
        Thread thread2 = new Thread(() -> {
            for (int j = 0; j < totalCount; j++) {
                count.incrementAndGet();
            }
        });
        thread1.start();
        thread2.start();

        thread1.join();
        thread2.join();

        System.out.println(count.get());


        //王五
        boolean result = atomicStampedReference.compareAndSet("", "王五",
                1, 2);
        System.out.println(result);
        result = atomicStampedReference.compareAndSet("王五", "",
                2, 3);
        System.out.println(result);
        //张三回来了
        result = atomicStampedReference.compareAndSet("", "张三",
                1, 2);
        System.out.println(result);

        System.out.println(atomicStampedReference.getReference());
    }
}
